{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "dates = pd.date_range('20200731', periods=6)\n",
    "df = pd.DataFrame(np.random.randn(6,4),\n",
    "                  index = dates, # 列索引\n",
    "                  columns = ['a', 'b','c', 'd']) # 行索引\n",
    "df.iloc[0,1] = np.nan\n",
    "df.iloc[1,2] = np.nan"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "      <th>c</th>\n",
       "      <th>d</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2020-07-31</th>\n",
       "      <td>0.587994</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.847835</td>\n",
       "      <td>-0.469151</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-08-01</th>\n",
       "      <td>0.333378</td>\n",
       "      <td>0.495154</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.129092</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-08-02</th>\n",
       "      <td>0.245048</td>\n",
       "      <td>0.079800</td>\n",
       "      <td>0.299207</td>\n",
       "      <td>-0.120899</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-08-03</th>\n",
       "      <td>-0.947855</td>\n",
       "      <td>0.032060</td>\n",
       "      <td>0.769018</td>\n",
       "      <td>0.906137</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-08-04</th>\n",
       "      <td>0.615286</td>\n",
       "      <td>0.360310</td>\n",
       "      <td>1.177011</td>\n",
       "      <td>-0.593868</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-08-05</th>\n",
       "      <td>-0.070682</td>\n",
       "      <td>-0.670453</td>\n",
       "      <td>-2.204943</td>\n",
       "      <td>-0.434558</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   a         b         c         d\n",
       "2020-07-31  0.587994       NaN  1.847835 -0.469151\n",
       "2020-08-01  0.333378  0.495154       NaN  0.129092\n",
       "2020-08-02  0.245048  0.079800  0.299207 -0.120899\n",
       "2020-08-03 -0.947855  0.032060  0.769018  0.906137\n",
       "2020-08-04  0.615286  0.360310  1.177011 -0.593868\n",
       "2020-08-05 -0.070682 -0.670453 -2.204943 -0.434558"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df  # NAN为丢失的数据"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 丢掉整/列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "      <th>c</th>\n",
       "      <th>d</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2020-08-02</th>\n",
       "      <td>0.245048</td>\n",
       "      <td>0.079800</td>\n",
       "      <td>0.299207</td>\n",
       "      <td>-0.120899</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-08-03</th>\n",
       "      <td>-0.947855</td>\n",
       "      <td>0.032060</td>\n",
       "      <td>0.769018</td>\n",
       "      <td>0.906137</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-08-04</th>\n",
       "      <td>0.615286</td>\n",
       "      <td>0.360310</td>\n",
       "      <td>1.177011</td>\n",
       "      <td>-0.593868</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-08-05</th>\n",
       "      <td>-0.070682</td>\n",
       "      <td>-0.670453</td>\n",
       "      <td>-2.204943</td>\n",
       "      <td>-0.434558</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   a         b         c         d\n",
       "2020-08-02  0.245048  0.079800  0.299207 -0.120899\n",
       "2020-08-03 -0.947855  0.032060  0.769018  0.906137\n",
       "2020-08-04  0.615286  0.360310  1.177011 -0.593868\n",
       "2020-08-05 -0.070682 -0.670453 -2.204943 -0.434558"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# axis = 0: 按行丢弃\n",
    "# how = 'any': 有任意一列的数据为NAN就丢掉\n",
    "df.dropna(axis=0, how='any')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "      <th>c</th>\n",
       "      <th>d</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2020-07-31</th>\n",
       "      <td>0.587994</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.847835</td>\n",
       "      <td>-0.469151</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-08-01</th>\n",
       "      <td>0.333378</td>\n",
       "      <td>0.495154</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.129092</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-08-02</th>\n",
       "      <td>0.245048</td>\n",
       "      <td>0.079800</td>\n",
       "      <td>0.299207</td>\n",
       "      <td>-0.120899</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-08-03</th>\n",
       "      <td>-0.947855</td>\n",
       "      <td>0.032060</td>\n",
       "      <td>0.769018</td>\n",
       "      <td>0.906137</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-08-04</th>\n",
       "      <td>0.615286</td>\n",
       "      <td>0.360310</td>\n",
       "      <td>1.177011</td>\n",
       "      <td>-0.593868</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-08-05</th>\n",
       "      <td>-0.070682</td>\n",
       "      <td>-0.670453</td>\n",
       "      <td>-2.204943</td>\n",
       "      <td>-0.434558</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   a         b         c         d\n",
       "2020-07-31  0.587994       NaN  1.847835 -0.469151\n",
       "2020-08-01  0.333378  0.495154       NaN  0.129092\n",
       "2020-08-02  0.245048  0.079800  0.299207 -0.120899\n",
       "2020-08-03 -0.947855  0.032060  0.769018  0.906137\n",
       "2020-08-04  0.615286  0.360310  1.177011 -0.593868\n",
       "2020-08-05 -0.070682 -0.670453 -2.204943 -0.434558"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# axis = 0: 按行丢弃\n",
    "# how = 'all': 所有列的数据为NAN才丢掉\n",
    "df.dropna(axis=0, how='all')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 填充"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "      <th>c</th>\n",
       "      <th>d</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2020-07-31</th>\n",
       "      <td>0.587994</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.847835</td>\n",
       "      <td>-0.469151</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-08-01</th>\n",
       "      <td>0.333378</td>\n",
       "      <td>0.495154</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.129092</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-08-02</th>\n",
       "      <td>0.245048</td>\n",
       "      <td>0.079800</td>\n",
       "      <td>0.299207</td>\n",
       "      <td>-0.120899</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-08-03</th>\n",
       "      <td>-0.947855</td>\n",
       "      <td>0.032060</td>\n",
       "      <td>0.769018</td>\n",
       "      <td>0.906137</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-08-04</th>\n",
       "      <td>0.615286</td>\n",
       "      <td>0.360310</td>\n",
       "      <td>1.177011</td>\n",
       "      <td>-0.593868</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-08-05</th>\n",
       "      <td>-0.070682</td>\n",
       "      <td>-0.670453</td>\n",
       "      <td>-2.204943</td>\n",
       "      <td>-0.434558</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   a         b         c         d\n",
       "2020-07-31  0.587994  1.000000  1.847835 -0.469151\n",
       "2020-08-01  0.333378  0.495154  1.000000  0.129092\n",
       "2020-08-02  0.245048  0.079800  0.299207 -0.120899\n",
       "2020-08-03 -0.947855  0.032060  0.769018  0.906137\n",
       "2020-08-04  0.615286  0.360310  1.177011 -0.593868\n",
       "2020-08-05 -0.070682 -0.670453 -2.204943 -0.434558"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.fillna(value = 1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 判断是否有数据丢失"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "      <th>c</th>\n",
       "      <th>d</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2020-07-31</th>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-08-01</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-08-02</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-08-03</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-08-04</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-08-05</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                a      b      c      d\n",
       "2020-07-31  False   True  False  False\n",
       "2020-08-01  False  False   True  False\n",
       "2020-08-02  False  False  False  False\n",
       "2020-08-03  False  False  False  False\n",
       "2020-08-04  False  False  False  False\n",
       "2020-08-05  False  False  False  False"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.isnull()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.any(df.isnull())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
